﻿using System;
using System.Collections.Generic;
using System.Web;
namespace GetTogether.Studio.WebService
{
    public class WebServiceProxyHelper
    {
        public enum ProxyType
        {
            Cache = 0,
            Case = 1,
        }

        public static string GetMethodName(string requestXml)
        {
            System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(@"<soap:Body><(?<MethodName>[A-Z|0-9|_]{1,})([ ]|\>)", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            System.Text.RegularExpressions.Match m = regx.Match(requestXml);
            if (m.Success)
            {
                return m.Groups["MethodName"].Value;
            }
            return "";
        }

        public static string GetRootURI(HttpRequest request)
        {
            string rootURI = "";
            if (request != null)
            {
                string UrlAuthority = request.Url.GetLeftPart(UriPartial.Authority);
                if (request.ApplicationPath == null || request.ApplicationPath == "/")
                    rootURI = UrlAuthority;
                else
                    rootURI = UrlAuthority + request.ApplicationPath;
            }
            return string.Concat(rootURI, "/");
        }

        public static string SaveProxy(string userCode, string projName, string methodName, string proxyName, string regular, string requestXml, string responseXml)
        {
            string proxyPath = WebServiceProxyHelper.GetProxyPath(userCode, projName, methodName);
            proxyPath = System.IO.Path.Combine(proxyPath, proxyName);
            if (!System.IO.Directory.Exists(proxyPath)) System.IO.Directory.CreateDirectory(proxyPath);
            System.IO.File.WriteAllText(System.IO.Path.Combine(proxyPath, "Request.txt"), requestXml);
            System.IO.File.WriteAllText(System.IO.Path.Combine(proxyPath, "Response.txt"), responseXml);
            ProxyCaseParameter proxyParam = new ProxyCaseParameter();
            proxyParam.ProxyName = proxyName;
            proxyParam.Regular = regular;
            System.IO.File.WriteAllText(System.IO.Path.Combine(proxyPath, "Parameter.txt"), GetTogether.Utility.SerializationHelper.SerializeToXml(proxyParam));
            return string.Empty;
        }

        public static string GetProxyPath(string userCode, string projName, string methodName)
        {
            string path = GetTogether.Studio.WebService.ProjectParameter.GetSettingsPath(userCode);
            path = System.IO.Path.Combine(path, string.Concat(projName, "(Proxy)\\", methodName));
            return path;
        }

        public static List<string> GetProxyCases(string userCode, string projName, string methodName)
        {
            string proxyPath = WebServiceProxyHelper.GetProxyPath(userCode, projName, methodName);
            if (System.IO.Directory.Exists(proxyPath))
            {
                List<string> ret = new List<string>();
                foreach (string d in System.IO.Directory.GetDirectories(proxyPath))
                {
                    ret.Add(GetFolderName(d));
                }
                return ret;
            }
            return null;
        }

        public static string GetProxyCaseXml(string userCode, string projName, string methodName, string proxyName, string file)
        {
            try
            {
                string filePath = System.IO.Path.Combine(WebServiceProxyHelper.GetProxyPath(userCode, projName, methodName), proxyName);
                filePath = System.IO.Path.Combine(filePath, file);
                return System.IO.File.ReadAllText(filePath, System.Text.Encoding.UTF8);
            }
            catch (Exception ex)
            {
                GetTogether.Studio.Log.Error(ex);
                return ex.ToString();
            }
        }

        protected static string GetFolderName(string path)
        {
            if (string.IsNullOrEmpty(path)) return string.Empty;
            string[] pathInfo = path.Split('\\');
            return pathInfo[pathInfo.Length - 1];
        }
    }

    public class ProxyCaseParameter
    {
        public string ProxyName;
        public string Regular;
        public ProxyCaseParameter()
        {

        }
    }
}